class ApplicationController < ActionController::Base
  protect_from_forgery
  
  
  helper_method :current_user

  private
  
    def current_user
      @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end
    
    def current_collection
      @current_collection ||= Collection.find(session[:collection_id]) if session[:collection_id]
    end
    
    def destroy_collection_session
      @current_collection = nil
      session[:collection_id] = nil
    end
    
    def current_book
      @current_book ||= Book.find(session[:book_id]) if session[:book_id]
    end
    
    def destroy_book_session
      session[:book_id] = nil
      @current_book = nil
    end
    
    def destroy_user_session
      session[:user_id] = nil
      @current_user = nil
    end
    
    def redirect_to_login
      flash.now.alert = "Log as admin"
      respond_to do |format|
        format.html { render "sessions/new" }
        return
      end
    end
    
    def not_an_admin
        respond_to do |format|
          format.html { render "home/index" }
        end
      return
    end
    
    def admin?
      current_user && current_user.admin
    end
    
  
end
